package com.junlong0716.enjoylearning

import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.support.v4.app.Fragment
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.view.KeyEvent
import android.view.View
import com.junlong0716.enjoylearning.service.MusicService
import com.junlong0716.enjoylearning.service.PlayerManager
import com.junlong0716.enjoylearning.ui.FindFragment
import com.junlong0716.enjoylearning.ui.MineFragment
import com.junlong0716.enjoylearning.ui.PublishFragment
import com.junlong0716.enjoylearning.ui.StudyFragment
import com.junlong0716.enjoylearning.widget.PlayerBarView
import com.junlong0716.module.common.base.BaseActivity
import com.junlong0716.module.common.constant.Constant
import com.junlong0716.module.common.event.AudioLayoutVisibleStateEvent
import com.junlong0716.module.common.rxbus.Subscribe
import com.junlong0716.module.common.rxbus.ThreadMode
import com.junlong0716.module.common.utils.StatusBarUtil
import kotlinx.android.synthetic.main.activity_home.*
import kotlinx.android.synthetic.main.view_bottom_menu.*


class HomeActivity : BaseActivity<HomePresenter>(), HomeContract.View, View.OnClickListener {
    //init fragment
    private var findFragment: FindFragment? = null
    private var mineFragment: MineFragment? = null
    private var studyFragment: StudyFragment? = null
    private var publishFragment: PublishFragment? = null

    private lateinit var mPlayerBarView: PlayerBarView
    private var musicService: MusicService? = null
    private var mPlayerManager: PlayerManager? = null


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun eventBus(s: AudioLayoutVisibleStateEvent) {
        if (s.key == Constant.AUDIO_LAYOUT_VISIBLE_FLAG) {
            //先判断是否能显示播放控制bar
            if (mPlayerManager!!.getPlayerBarVisibilityState()) {
                if (s.isVisible) {
                    mPlayerBarView.setPlayerBarVisibility(true)
                } else {
                    mPlayerBarView.setPlayerBarVisibility(false)
                }
            }
        }
    }

    override fun beforeSetLayout() {
        StatusBarUtil.setColor(this, ContextCompat.getColor(this, R.color.common_transparent100))
    }

    override fun attachPresenter() {
        mPresenter = HomePresenter()
        mPresenter!!.attachView(this)
    }

    override fun getLayoutId(): Int = R.layout.activity_home

    private var conn = object : ServiceConnection {
        override fun onServiceDisconnected(name: ComponentName?) {

        }

        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            val myBinder = service as (MusicService.MyBinder)
            musicService = myBinder.getService()
            mPlayerManager = musicService!!.getPlayerManager()
            mPlayerBarView = PlayerBarView(this@HomeActivity, mPlayerManager!!, rl_root, R.id.bottom_bar)
        }
    }

    override fun bindService() {
        val intent = Intent(this, MusicService::class.java)
        bindService(intent, conn, AppCompatActivity.BIND_AUTO_CREATE)
    }

    override fun initView(savedInstanceState: Bundle?) {
        initListener()

        initTextViewAndImageView()

        initFragment(savedInstanceState)
    }

    private fun initTextViewAndImageView() {
        iv_find.setImageDrawable(ContextCompat.getDrawable(this, R.mipmap.ic_menu_find_normal))
        tv_find.setTextColor(ContextCompat.getColor(this, R.color.text_color_gray))
        iv_mine.setImageDrawable(ContextCompat.getDrawable(this, R.mipmap.ic_menu_mine_normal))
        tv_mine.setTextColor(ContextCompat.getColor(this, R.color.text_color_gray))
        iv_publish.setImageDrawable(ContextCompat.getDrawable(this, R.mipmap.ic_menu_publish_normal))
        tv_publish.setTextColor(ContextCompat.getColor(this, R.color.text_color_gray))
        iv_study.setImageDrawable(ContextCompat.getDrawable(this, R.mipmap.ic_menu_study_normal))
        tv_study.setTextColor(ContextCompat.getColor(this, R.color.text_color_gray))
    }

    private fun initListener() {
        rl_find.setOnClickListener(this)
        rl_publish.setOnClickListener(this)
        rl_mine.setOnClickListener(this)
        rl_study.setOnClickListener(this)
    }

    private fun initFragment(savedInstanceState: Bundle?) {
        if (savedInstanceState != null) {
            //异常情况
            val mFragments: List<Fragment> = supportFragmentManager.fragments
            for (item in mFragments) {
                if (item is FindFragment) {
                    findFragment = item
                }
                if (item is StudyFragment) {
                    studyFragment = item
                }
                if (item is PublishFragment) {
                    publishFragment = item
                }
                if (item is MineFragment) {
                    mineFragment = item
                }
            }
        } else {
            findFragment = FindFragment()
            studyFragment = StudyFragment()
            mineFragment = MineFragment()
            publishFragment = PublishFragment()
            val fragmentTrans = supportFragmentManager.beginTransaction()
            fragmentTrans.add(R.id.fl_container, publishFragment)
            fragmentTrans.add(R.id.fl_container, findFragment)
            fragmentTrans.add(R.id.fl_container, mineFragment)
            fragmentTrans.add(R.id.fl_container, studyFragment)
            fragmentTrans.commit()
        }
        supportFragmentManager.beginTransaction().show(findFragment)
                .hide(publishFragment)
                .hide(mineFragment)
                .hide(studyFragment)
                .commit()

        iv_find.setImageDrawable(ContextCompat.getDrawable(this, R.mipmap.ic_menu_find_selected))
        tv_find.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary))
    }

    override fun onClick(v: View?) {
        initTextViewAndImageView()
        when (v!!.id) {
            R.id.rl_find -> {
                iv_find.setImageDrawable(ContextCompat.getDrawable(this, R.mipmap.ic_menu_find_selected))
                tv_find.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary))
                supportFragmentManager.beginTransaction().show(findFragment)
                        .hide(publishFragment)
                        .hide(mineFragment)
                        .hide(studyFragment)
                        .commit()
            }
            R.id.rl_mine -> {
                iv_mine.setImageDrawable(ContextCompat.getDrawable(this, R.mipmap.ic_menu_mine_selected))
                tv_mine.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary))
                supportFragmentManager.beginTransaction().show(mineFragment)
                        .hide(publishFragment)
                        .hide(findFragment)
                        .hide(studyFragment)
                        .commit()
            }
            R.id.rl_publish -> {
                iv_publish.setImageDrawable(ContextCompat.getDrawable(this, R.mipmap.ic_menu_publish_selected))
                tv_publish.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary))
                supportFragmentManager.beginTransaction().show(publishFragment)
                        .hide(studyFragment)
                        .hide(findFragment)
                        .hide(mineFragment)
                        .commit()
            }
            R.id.rl_study -> {
                iv_study.setImageDrawable(ContextCompat.getDrawable(this, R.mipmap.ic_menu_study_selected))
                tv_study.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary))
                supportFragmentManager.beginTransaction().show(studyFragment)
                        .hide(publishFragment)
                        .hide(findFragment)
                        .hide(mineFragment)
                        .commit()
            }
            else -> {
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        unbindService(conn)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(false)
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    fun getPlayerManager(): PlayerManager {
        return mPlayerManager!!
    }
}
